/****************************************************************************************** Import */
import _ from 'lodash';
import React, {Component} from 'react';
import {View, TouchableHighlight, TouchableOpacity} from 'react-native';
import CompStyle from '../style';

/****************************************************************************************** Class */
export default class CompEventContainer extends CompStyle {

    /**
     * 判断是否存在事件
     */
    hasEvent() {
        if (this.sysProps.onPress || this.sysProps.onLongPress) {
            return true;
        }
        return false;
    }

    /**
     * 获取触摸组件类型
     */
    getTouchControlType() {
        if (this.sysProps.oncolor || !this.sysProps.onopacity) {
            return 'color';
        }
        return 'opacity';
    }

    /**
     * 设置color类型touch被点击样式
     */
    setSysPropsColor() {
        if (this.props.oncolor) {
            this.sysProps['underlayColor'] = this.props.oncolor;
            this.sysProps = _.omit(this.sysProps, ['oncolor']);
        }
        else {
            if (this.styles.backgroundColor) this.sysProps['underlayColor'] = this.styles.backgroundColor;
            else this.sysProps['underlayColor'] = 'transparent';
        }
    }

    /**
     * 设置opacity类型touch被点击样式
     */
    setSysPropsOpacity() {
        if (this.props.onopacity) {
            this.sysProps['activeOpacity'] = parseInt(this.props.onopacity);
            this.sysProps = _.omit(this.sysProps, ['onopacity']);
        }
        else this.sysProps['activeOpacity'] = 0.7;
    }

    /**
	 * 渲染
	 */
	render() {
		if (this.hasEvent()) {
			if (this.getTouchControlType() == 'color') {
				this.setSysPropsColor();
				return (
					<TouchableHighlight {...this.sysProps} style={this.styles}>
                        {this.props.children}
					</TouchableHighlight>
				);
			} else {
				this.setSysPropsOpacity();
				return (
					<TouchableOpacity {...this.sysProps} activeOpacity={0.7} style={this.styles}>
                        {this.props.children}
					</TouchableOpacity>
				);
			}
		}
		else {
			return (
				<View {...this.sysProps} style={this.styles}>
					{this.props.children}
				</View>
			);
		}		
	}
}
